import superService from "./superService";
import gameConfig from "../configs/gameConfig";
import logScreen from "../../O1ModuleRes/frame/logScreen";

//日志输出等级
export enum LOG_LEVEL {
    debug,
    log,
    info,
    warn,
    error,
    empty//无输出
    //group
    //groupEnd
}

export interface NeedAddText {
    text: string;
    color: cc.Color;
}

export default class LogService extends superService {
    //
    protected className: string = '日志服务';

    //日志等级
    private m_logLevel: LOG_LEVEL = LOG_LEVEL.debug;
    //
    private m_debug: (...args) => any;
    private m_log: (...args) => any;
    private m_info: (...args) => any;
    private m_warn: (...args) => any;
    private m_error: (...args) => any;
    //
    private m_screenLog: logScreen = null;
    //
    private m_needPrintText: NeedAddText[] = [];

    //
    constructor() {
        super();
    }

    //
    async loadService(): Promise<void> {
        await super.loadService();
        if (!gameConfig.LOG_SERVICE_OPEN) {
            return Promise.resolve();
        }
        this.m_logLevel = gameConfig.CONSOLE_LOG_LEVEL;
        //
        this.registerConsoleAgent();
    }

    //注册代理
    private registerConsoleAgent() {
        //
        this.m_debug = console.debug;
        this.m_log = console.log;
        this.m_info = console.info;
        this.m_warn = console.warn;
        this.m_error = console.error;
        //
        //监听屏幕打印
        console.error = this.onScreenPrint.bind(this, LOG_LEVEL.error);
        console.warn = this.onScreenPrint.bind(this, LOG_LEVEL.warn);
        console.info = this.onScreenPrint.bind(this, LOG_LEVEL.info);
        console.log = this.onScreenPrint.bind(this, LOG_LEVEL.log);
        console.debug = this.onScreenPrint.bind(this, LOG_LEVEL.debug);
        //
    }

    protected onScreenPrint(logLevel: LOG_LEVEL, ...args) {
        let color: cc.Color = cc.Color.WHITE;
        let text = '';
        let t = new Date();
        let time = `${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}.${t.getTime() % 1000}`;
        switch (logLevel) {
            case LOG_LEVEL.empty: {
                break;
            }
            case LOG_LEVEL.error: {
                this.m_error(text, ...args);
                text = '[ERROR]';
                text += `${time}:`;
                color = cc.color(0xEE, 0x00, 0x00);
                break;
            }
            case LOG_LEVEL.warn: {
                text = '[WARN]';
                text += `${time}:`;
                this.m_warn(text, ...args);
                color = cc.color(0xee, 0xee, 0x00);
                break;
            }
            case LOG_LEVEL.info: {
                text = '[INFO]';
                text += `${time}:`;
                this.m_info(text, ...args);
                color = cc.color(0x00, 0xee, 0x00);
                break;
            }
            case LOG_LEVEL.log: {
                text = '[LOG]';
                text += `${time}:`;
                this.m_log(text, ...args);
                color = cc.color(0xff, 0xff, 0xff);
                break;
            }
            case LOG_LEVEL.debug: {
                text = '[DEBUG]';
                text += `${time}:`;
                this.m_debug(text, ...args);
                color = cc.color(0xcc, 0xcc, 0xcc);
                break;
            }
        }
        //合并输出
        args.forEach(value => {
            if (typeof value == 'object') {
                try {
                    text += JSON.stringify(value);
                } catch (e) {
                    text += value.toString();
                }
            } else {
                text += value;
            }
        });
        //添加日志
        if (!this.m_screenLog) {
            this.m_needPrintText.push({text, color});
            return;
        }
        if (this.m_needPrintText.length > 0) {
            this.m_needPrintText.forEach(value => {
                this.m_screenLog.addLog(value.text, value.color);
            })
            this.m_needPrintText.length = 0;
        }
        this.m_screenLog.addLog(text, color);
    }

    //添加屏幕打印
    addScreenPrint(logComponent: logScreen) {
        this.m_screenLog = logComponent;
    }
}































